---
title: Running Tests In Parallel
layout: page
pageOrder: 9
section: 'Mock Server'
subsection: true
sitemap:
  priority: 0.7
  changefreq: 'monthly'
  lastmod: 2018-11-16T16:00:00+01:00
---

<p>To ensure all tests can run in parallel with completely isolated independent data it is important to include a value unique to each test for the request matcher. </p>

<p>If each test generates a unique value (i.e. a UUID) for the sessionId cookie or a correlationId header then each test can receive completely independent response.</p>



<p><strong>Java</strong></p>

<pre class="prettyprint lang-java code"><code class="code">String sessionId = UUID.randomUUID().toString();

new MockServerClient("127.0.0.1", 1080)
    .when(
        request()
            .withMethod("GET")
            .withPath("/somePath")
            .withCookies(
                cookie("sessionId", sessionId)
            )
    )
    .respond(
        response()
            .withStatusCode(200)
            .withBody("{ name: 'value' }")
    );</code></pre>

<p><strong>JavaScript</strong></p>

<pre class="prettyprint lang-javascript code"><code class="code">function guid() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    }
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
        s4() + '-' + s4() + s4() + s4();
}

var sessionId = guid();

mockServerClient("localhost", 1080)
    .mockAnyResponse({
        'httpRequest': {
            'method': 'GET',
            'path': '/somePath',
            "cookies": {
                "sessionId": sessionId
            }
        },
        'httpResponse': {
            'statusCode': 200,
            'body': JSON.stringify({name: 'value'})
        }
    })
    .then(
        function () {
            console.log("expectation created");
        },
        function (error) {
            console.log(error);
        }
    );</code></pre>
